//
//  VCMore.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/22.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCMore: VCBase {
    
    // 头部布局
    private lazy var moreHeaderView: MoreHeaderView = {
        let ui = MoreHeaderView()
        ui.delegate = self
        ui.layer.cornerRadius = 8
        ui.layer.masksToBounds = true
        ui.backgroundColor = UIColor.white
        ui.frame = CGRect(x: MARGIN, y: realHeight + MARGIN5, width: S_16_M, height: 110)
        return ui
    }()
    
    // 躯体布局
    private lazy var moreBodyView:MoreBodyView = {
        let ui = MoreBodyView()
        let uiY = moreHeaderView.frame.maxY + MARGIN10
        ui.frame = CGRect(x: MARGIN, y: uiY, width: S_16_M, height: SCREEN_HEIGHT - uiY)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_data()
    }
}

// MARK: 代理
extension VCMore:MoreHeaderViewDelegate {
    
    // 切换城市
    func search_switchCity() {
        let city = moreHeaderView.cityView.currentTitle!
        let endcity = moreHeaderView.endcityView.currentTitle!
        // 更新
        self.requestData(city: city, endcity: endcity)
    }
    
    // 出发城市点击
    func search_StartCity() {
        let vc = VCCitySelect()
        vc.title = "选择出发城市"
        vc.complete = {city in
            self.moreHeaderView.resetStartCity = city
            self.requestData(city: city, endcity: self.moreHeaderView.endcityView.currentTitle!)
        }
        self.pushNextVcHideBottomBar(vc: vc)
    }
    
    // 到达城市点击
    func search_stopCity() {
        let vc = VCCitySelect()
        vc.title = "选择到达城市"
        vc.complete = {city in
            self.moreHeaderView.resetStopCity = city
            self.requestData(city: self.moreHeaderView.cityView.currentTitle!, endcity: city)
        }
        self.pushNextVcHideBottomBar(vc: vc)
    }
}

// MARK: 初始化
extension VCMore {
    
    func requestData(city:String,endcity:String) -> Void {
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(city, forKey: "city")
        request.setValue(endcity, forKey: "endcity")
        // 请求
        self.doPost(api: "search/specailticket", dict: request) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                self.noDataView.isHidden = false
                self.moreBodyView.isHidden = true
                self.noDataView.resetTipText = "未搜到相应航班,请重新选择"
                self.view.addSubview(self.noDataView)
                return
            }
            self.noDataView.isHidden = true
            self.moreBodyView.isHidden = false
            // 解析数据
            guard let result = dict.value(forKey: SP_BODYREQUEST) as? NSDictionary else {
                return
            }
            // 特价机票
            if let specials = result.value(forKey: "specials") as? NSArray {
                var temp = [SpecialTicketInfo]()
                for item in specials {
                    let ticket = SpecialTicketInfo(dict: item as! [String : Any])
                    temp.append(ticket)
                }
                self.moreBodyView.ticketInfos = temp
            }
        }
    }
    
    private func init_data() -> Void {
        // 请求数据
        requestData(city: "北京", endcity: "上海")
        // 条目点击
        moreBodyView.complete = {item in
            let dict = NSMutableDictionary()
            dict.setValue(item.city, forKey: "city");
            dict.setValue(item.endcity, forKey: "endcity");
            dict.setValue(item.departdate, forKey: "departdate");
            dict.setValue(item.flightnumberid, forKey: "flightnumberid");
            // 跳转界面
            let vc = VCShowTicket()
            vc.requestDict = dict
            vc.title = "\(item.city!) - \(item.endcity!)"
            self.pushNextVcHideBottomBar(vc: vc)
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backgroundImg)
        view.addSubview(moreHeaderView)
        view.addSubview(moreBodyView)
    }
}
